<?php
class contact extends database{
// add ------
	public function Add($name, $email, $phone, $content, $status){				
		if(!$this->isText($name)){
			return 3;
		}else if(!$this->isText($email)){
			return 4;
		}else if(!$this->isText($phone)){
			return 5;
		}else if(!$this->isText($content)){
			return 6;
		}else if(!$this->isInt($status)){
			return 7;
		}else{			
			$sql="insert into contact(name, email, phone, content, status, date) values('".$name."', '".$email."', '".$phone."', '".$content."', ".$status.", now())";		
			if($result=$this->query($sql)){
				return 1;
			}else{
				return 0;
			}
			return -1;
		}
	}
// edit ------
	public function Edit($id, $status){				
		if(!$this->isChar($id)){
			return 2;
		}else if(!$this->isInt($status)){
			return 7;
		}else{			
			echo $sql="update contact set status=".$status." where id=".$id."";		
			if($result=$this->query($sql)){
				return 1;
			}else{
				return 0;
			}
			return -1;
		}
	}
// Del ------
	public function Del($id){				
		if(!$this->isInt($id)){
			return 2;
		}else{			
			$sql="delete from contact where id=".$id."";		
			if($result=$this->query($sql)){
				return 1;
			}else{
				return 0;
			}
		}
	}
// get by id
	public function GetById($id){	
		if(!$this->isInt($id)){
			return 2;
		}else{	
			$sql="select id, name, email, phone, content, status, date from contact where id=".$id."";
			if($result=$this->query($sql)){
				if($this->CountRow($result)>0){
					while(($a=$this->fetch($result))!=NULL){
						$ds_sp[]=$this->unvalid($a); 
					}
					return $ds_sp;		
				}else{
					return 1;
				}
			}
		}
		return 0;
	}
// get all
	public function GetByAll($Top=0, $Position=0, $status=10){	
		if(!$this->isInt($Top) || !$this->isInt($Position) ){
			return 2;	
		}else if(!$this->isInt($status)){
			return 3;
		}else{				
			if($status==10){
				$status	= " status<>2";	
			}else{
				$status	= " status=".$status;	
			}
			if($Top>0){
				$Top=" limit ".$Position.", ".$Top." ";
			}else{
				$Top="";
			}
			$sql="select id, name, email, phone, content, status, date from contact where ".$status." order by id desc".$Top;
			if($result=$this->query($sql)){
				if($this->CountRow($result)>0){
					while(($a=$this->fetch($result))!=NULL){
						$ds_sp[]=$this->unvalid($a); 
					}
					return $ds_sp;		
				}else{
					return 1;
				}
			}
		}
		return 0;
	}
// get MaxIdCount
	public function CountByAll($status=10){		
		if(!$this->isInt($status)){
			return 4;
		}
		if($status==10){
			$status	= " status<>2";	
		}else{
			$status	= " status=".$status;	
		}
		$sql="select count(*) as count from contact where ".$status."";
		if($result=$this->query($sql)){
			$a=$this->fetch($result);
			return $a['count'];
		}
		return 0;
	}
}
?>